home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Direct3D / VertexBlend / vertexblend.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-08  |  12.6 KB  |  328 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Vertex Blend"
  4.    ClientHeight    =   4485
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   5640
  8.    Icon            =   "vertexblend.frx":0000
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   299
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   376
  13.    StartUpPosition =   3  'Windows Default
  14. Attribute VB_Name = "Form1"
  15. Attribute VB_GlobalNameSpace = False
  16. Attribute VB_Creatable = False
  17. Attribute VB_PredeclaredId = True
  18. Attribute VB_Exposed = False
  19. '-----------------------------------------------------------------------------
  20. ' File: VertexBlend.frm
  21. ' Desc: Example code showing how to do a skinning effect, using the vertex
  22. '       blending feature of Direct3D. Normally, Direct3D transforms each
  23. '       vertex through the world matrix. The vertex blending feature,
  24. '       however, uses mulitple world matrices and a per-vertex blend factor
  25. '       to transform each vertex.
  26. ' Copyright (C) 1999-2001 Microsoft Corporation. All rights reserved.
  27. '-----------------------------------------------------------------------------
  28. Option Explicit
  29. Const D3DVBF_DISABLE = 0    '     Disable vertex blending
  30. Const D3DVBF_1WEIGHTS = 1   '     2 matrix blending
  31. Const D3DVBF_2WEIGHTS = 2   '     3 matrix blending
  32. Const D3DVBF_3WEIGHTS = 3   '     4 matrix blending
  33. Const D3DVBF_0WEIGHTS = 256 '     one matrix is used with weight 1.0
  34. '-----------------------------------------------------------------------------
  35. ' Name: struct D3DBLENDVERTEX
  36. ' Desc: Custom vertex which includes a blending factor
  37. '-----------------------------------------------------------------------------
  38. Private Type D3DBLENDVERTEX
  39.     v As D3DVECTOR
  40.     blend As Single
  41.     n As D3DVECTOR
  42.     tu As Single
  43.     tv As Single
  44. End Type
  45. Const D3DFVF_BLENDVERTEX = (D3DFVF_XYZB1 Or D3DFVF_NORMAL Or D3DFVF_TEX1)
  46. Dim m_Object As CD3DMesh
  47. Dim m_matUpperArm As D3DMATRIX
  48. Dim m_matLowerArm As D3DMATRIX
  49. Dim m_mediadir As String
  50. Dim g_ftime As Single
  51. Dim m_bInit As Boolean                  ' Indicates that d3d has been initialized
  52. Dim m_bMinimized As Boolean             ' Indicates that display window is minimized
  53. '-----------------------------------------------------------------------------
  54. ' Name: Form_Load()
  55. ' Desc:
  56. '-----------------------------------------------------------------------------
  57. Private Sub Form_Load()
  58.     ' Show the form
  59.     Me.Show
  60.     DoEvents
  61.     Me.Caption = "VertexBlend: Surface Skinning Example"
  62.         
  63.     ' Initialize D3D
  64.     ' Note: D3DUtil_Init will attempt to use D3D Hardware acceleartion.
  65.     ' If it is not available it attempt to use the Software Reference Rasterizer.
  66.     ' If all fail it will display a message box indicating so.
  67.     '
  68.     m_bInit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Nothing)
  69.     If Not (m_bInit) Then End
  70.             
  71.     ' Find and set the path to our media
  72.     m_mediadir = FindMediaDir("mslogo.x")
  73.     D3DUtil_SetMediaPath m_mediadir
  74.     ' Create new D3D mesh objects and loads content from disk
  75.     InitDeviceObjects
  76.     ' Sets the state for those objects and the current D3D device
  77.     RestoreDeviceObjects
  78.     ' Start our timer
  79.     DXUtil_Timer TIMER_start
  80.     ' Run the simulation forever
  81.     ' See Form_Keydown for exit processing
  82.     Do While True
  83.         ' Increment the simulation
  84.         FrameMove
  85.         
  86.         ' Render one image of the simulation
  87.         If Render Then
  88.         
  89.             ' Present the image to the screen
  90.             D3DUtil_PresentAll g_focushwnd
  91.         End If
  92.         
  93.         ' Allow for events to get processed
  94.         DoEvents
  95.         
  96.     Loop
  97. End Sub
  98. '-----------------------------------------------------------------------------
  99. ' Name: Form_KeyDown()
  100. ' Desc: Process key messages for exit and change device
  101. '-----------------------------------------------------------------------------
  102. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  103.      Select Case KeyCode
  104.         
  105.         Case vbKeyEscape
  106.             Unload Me
  107.             
  108.         Case vbKeyF2
  109.                 
  110.             ' Pause the timer
  111.             DXUtil_Timer TIMER_STOP
  112.             
  113.             ' Bring up the device selection dialog
  114.             ' we pass in the form so the selection process
  115.             ' can make calls into InitDeviceObjects
  116.             ' and RestoreDeviceObjects
  117.             frmSelectDevice.SelectDevice Me
  118.             
  119.             ' Restart the timer
  120.             DXUtil_Timer TIMER_start
  121.             
  122.         Case vbKeyReturn
  123.         
  124.             ' Check for Alt-Enter if not pressed exit
  125.             If Shift <> 4 Then Exit Sub
  126.             
  127.             ' If we are windowed go fullscreen
  128.             ' If we are fullscreen returned to windowed
  129.             If g_d3dpp.Windowed Then
  130.                  D3DUtil_ResetFullscreen
  131.             Else
  132.                  D3DUtil_ResetWindowed
  133.             End If
  134.                              
  135.             ' Call Restore after ever mode change
  136.             ' because calling reset looses state that needs to
  137.             ' be reinitialized
  138.             RestoreDeviceObjects
  139.            
  140.     End Select
  141. End Sub
  142. '-----------------------------------------------------------------------------
  143. ' Name: Form_Resize()
  144. ' Desc: hadle resizing of the D3D backbuffer
  145. '-----------------------------------------------------------------------------
  146. Private Sub Form_Resize()
  147.     ' If D3D is not initialized then exit
  148.     If Not m_bInit Then Exit Sub
  149.     ' If we are in a minimized state stop the timer and exit
  150.     If Me.WindowState = vbMinimized Then
  151.         DXUtil_Timer TIMER_STOP
  152.         m_bMinimized = True
  153.         Exit Sub
  154.         
  155.     ' If we just went from a minimized state to maximized
  156.     ' restart the timer
  157.     Else
  158.         If m_bMinimized = True Then
  159.             DXUtil_Timer TIMER_start
  160.             m_bMinimized = False
  161.         End If
  162.     End If
  163.     ' Dont let the window get too small
  164.     If Me.ScaleWidth < 10 Then
  165.         Me.width = Screen.TwipsPerPixelX * 10
  166.         Exit Sub
  167.     End If
  168.     If Me.ScaleHeight < 10 Then
  169.         Me.height = Screen.TwipsPerPixelY * 10
  170.         Exit Sub
  171.     End If
  172.     'reset and resize our D3D backbuffer to the size of the window
  173.     D3DUtil_ResizeWindowed Me.hwnd
  174.     'All state get losts after a reset so we need to reinitialze it here
  175.     g_lWindowWidth = Me.ScaleWidth
  176.     g_lWindowHeight = Me.ScaleHeight
  177.     RestoreDeviceObjects
  178. End Sub
  179. '-----------------------------------------------------------------------------
  180. ' Name: Form_Unload()
  181. ' Desc:
  182. '-----------------------------------------------------------------------------
  183. Private Sub Form_Unload(Cancel As Integer)
  184.     DeleteDeviceObjects
  185.     End
  186. End Sub
  187. '-----------------------------------------------------------------------------
  188. ' Name: FrameMove()
  189. ' Desc: Called once per frame, the call is the entry point for animating
  190. '       the scene.
  191. '-----------------------------------------------------------------------------
  192. Sub FrameMove()
  193.     g_ftime = DXUtil_Timer(TIMER_GETAPPTIME)
  194.     ' Set the vertex blending matrices for this frame
  195.     D3DXMatrixIdentity m_matUpperArm
  196.     Dim vAxis As D3DVECTOR
  197.     vAxis = vec3(2 + Sin(g_ftime * 3.1), 2 + Sin(g_ftime * 3.3), Sin(g_ftime * 3.5))
  198.     D3DXMatrixRotationAxis m_matLowerArm, vAxis, Sin(3 * g_ftime)
  199. End Sub
  200. '-----------------------------------------------------------------------------
  201. ' Name: Render()
  202. ' Desc: Called once per frame, the call is the entry point for 3d
  203. '       rendering. This function sets up render states, clears the
  204. '       viewport, and renders the scene.
  205. '-----------------------------------------------------------------------------
  206. Function Render() As Boolean
  207.     Dim hr As Long
  208.     Render = False
  209.     'See what state the device is in.
  210.     hr = g_dev.TestCooperativeLevel
  211.     If hr = D3DERR_DEVICENOTRESET Then
  212.         g_dev.Reset g_d3dpp
  213.         RestoreDeviceObjects
  214.     End If
  215.     'dont bother rendering if we are not ready yet
  216.     If hr <> 0 Then Exit Function
  217.     Render = True
  218.     ' Clear the backbuffer
  219.     D3DUtil_ClearAll &HFF&
  220.     With g_dev
  221.         .BeginScene
  222.         
  223.         ' Enable vertex blending
  224.         .SetRenderState D3DRS_VERTEXBLEND, D3DVBF_1WEIGHTS
  225.         .SetTransform D3DTS_WORLD, m_matUpperArm
  226.         .SetTransform D3DTS_WORLD1, m_matLowerArm
  227.         
  228.         ' Display the object
  229.         m_Object.Render g_dev
  230.         ' End the scene.
  231.         .EndScene
  232.     End With
  233. End Function
  234. '-----------------------------------------------------------------------------
  235. ' Name: InitDeviceObjects()
  236. ' Desc: Initialize scene objects.
  237. '-----------------------------------------------------------------------------
  238. Function InitDeviceObjects() As Boolean
  239.     Dim b As Boolean
  240.     Set m_Object = New CD3DMesh
  241.     b = m_Object.InitFromFile(g_dev, m_mediadir + "mslogo.x")
  242.     If Not b Then
  243.         MsgBox "media not found"
  244.         End
  245.     End If
  246.     ' Set a custom FVF for the mesh
  247.     m_Object.SetFVF g_dev, D3DFVF_BLENDVERTEX
  248.     Dim VertB As Direct3DVertexBuffer8
  249.     Dim Vertices() As D3DBLENDVERTEX
  250.     Dim NumVertices As Long
  251.     Dim MinX As Single
  252.     Dim MaxX As Single
  253.     Dim a As Single
  254.     Dim i As Long
  255.     NumVertices = m_Object.mesh.GetNumVertices()
  256.     Set VertB = m_Object.mesh.GetVertexBuffer()
  257.     MinX = 10000000000#
  258.     MaxX = -10000000000#
  259.     ReDim Vertices(NumVertices)
  260.     'copy data into our own array
  261.     D3DVertexBuffer8GetData VertB, 0, NumVertices * Len(Vertices(0)), 0, Vertices(0)
  262.     ' Calculate the min/max z values for all the vertices
  263.     For i = 0 To NumVertices - 1
  264.         If Vertices(i).v.x < MinX Then MinX = Vertices(i).v.x
  265.         If Vertices(i).v.x > MaxX Then MaxX = Vertices(i).v.x
  266.     Next
  267.     ' Set the blend factors for the vertices
  268.     For i = 0 To NumVertices - 1
  269.         a = (Vertices(i).v.x - MinX) / (MaxX - MinX)
  270.         Vertices(i).blend = 1 - Sin(a * g_pi * 1)
  271.     Next
  272.     D3DVertexBuffer8SetData VertB, 0, NumVertices * Len(Vertices(0)), 0, Vertices(0)
  273.     Set VertB = Nothing
  274.     InitDeviceObjects = True
  275. End Function
  276. '-----------------------------------------------------------------------------
  277. ' Name: RestoreDeviceObjects()
  278. ' Desc: Restore device-memory objects and state after a device is created or
  279. '       resized.
  280. '-----------------------------------------------------------------------------
  281. Sub RestoreDeviceObjects()
  282.     ' Restore mesh's local memory objects
  283.     m_Object.RestoreDeviceObjects g_dev
  284.     ' Set miscellaneous render states
  285.     With g_dev
  286.         .SetRenderState D3DRS_ZENABLE, 1  'TRUE
  287.         .SetRenderState D3DRS_AMBIENT, &H444444
  288.         
  289.         ' Set the projection matrix
  290.         Dim matProj As D3DMATRIX
  291.         D3DXMatrixPerspectiveFovLH matProj, g_pi / 4, Me.ScaleHeight / Me.ScaleWidth, 1#, 10000#
  292.         .SetTransform D3DTS_PROJECTION, matProj
  293.         ' Set the app view matrix for normal viewing
  294.         Dim vEyePt As D3DVECTOR, vLookatPt As D3DVECTOR, vUpVec As D3DVECTOR
  295.         Dim matView As D3DMATRIX
  296.         vEyePt = vec3(0#, -5#, -10#)
  297.         vLookatPt = vec3(0#, 0#, 0#)
  298.         vUpVec = vec3(0#, 1#, 0#)
  299.         D3DXMatrixLookAtLH matView, vEyePt, vLookatPt, vUpVec
  300.         .SetTransform D3DTS_VIEW, matView
  301.         ' Create a directional light
  302.         Dim light As D3DLIGHT8
  303.         D3DUtil_InitLight light, D3DLIGHT_DIRECTIONAL, 1, -1, 1
  304.         .SetLight 1, light
  305.         .LightEnable 1, 1  'True
  306.         .SetRenderState D3DRS_LIGHTING, 1 'TRUE
  307.         
  308.         
  309.     End With
  310. End Sub
  311. '-----------------------------------------------------------------------------
  312. ' Name: InvalidateDeviceObjects()
  313. ' Desc: Called when the device-dependant objects are about to be lost.
  314. '-----------------------------------------------------------------------------
  315. Sub InvalidateDeviceObjects()
  316.     m_Object.InvalidateDeviceObjects
  317. End Sub
  318. '-----------------------------------------------------------------------------
  319. ' Name: DeleteDeviceObjects()
  320. ' Desc: Called when the app is exitting, or the device is being changed,
  321. '       this function deletes any device dependant objects.
  322. '-----------------------------------------------------------------------------
  323. Sub DeleteDeviceObjects()
  324.     m_Object.Destroy
  325.     Set m_Object = Nothing
  326.     m_bInit = False
  327. End Sub
  328.